/*
 * @Description: 
 * @Author: 于海龙
 * @Date: 2024-10-08 16:33:30
 * @LastEditors: 于海龙
 * @LastEditTime: 2024-10-09 10:27:42
 */
import java.util.NoSuchElementException;

public class MyLinkedList<T> {
    private class Node<T> {
        private T data;
        private Node<T> pre;
        private Node<T> next;

        public Node(T data) {
            this.data = data;
            this.pre = null;
            this.next = null;
        }
    }

    private Node<T> first;
    private Node<T> last;
    private int size;

    // 添加元素方法
    public void add(T t) {
        Node<T> newNode = new Node<>(t);
        if (first == null) {
            first = newNode;
            last = newNode;
        } else {
            last.next = newNode;
            newNode.pre = last;
            last = newNode;
        }
        size++;
    }

    // 将元素添加到指定索引方法
    public void add(int index, T t) {
        checkIndex(index);
        if (index == size) {
            add(t);
        } else {
            Node<T> newNode = new Node<>(t);
            Node<T> curNode = getNode(index);
            if (curNode!= null) {
                newNode.pre = curNode.pre;
                newNode.next = curNode;
                if (curNode.pre!= null) {
                    curNode.pre.next = newNode;
                } else {
                    first = newNode;
                }
                curNode.pre = newNode;
            }
            size++;
        }
    }

    // 根据索引删除元素方法
    public void remove(int index) {
        checkIndex(index);
        Node<T> node = getNode(index);
        if (node!= null) {
            if (node.pre!= null) {
                node.pre.next = node.next;
            } else {
                first = node.next;
            }
            if (node.next!= null) {
                node.next.pre = node.pre;
            } else {
                last = node.pre;
            }
            size--;
        }
    }

    // 根据索引获取元素方法
    private Node<T> getNode(int index) {
        if (index < 0 || index >= size) {
            throw new NoSuchElementException();
        }
        Node<T> node;
        if (index < size / 2) {
            node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
        } else {
            node = last;
            for (int i = size - 1; i > index; i--) {
                node = node.pre;
            }
        }
        return node;
    }

    // 获取 MyLinkedList 长度方法
    public int getSize() {
        return this.size;
    }

    // 展示数据方法
    public void show() {
        for (int i = 0; i < getSize(); i++) {
            Node<T> node = getNode(i);
            if (node!= null) {
                if (i == getSize() - 1) {
                    System.out.println(node.data);
                } else {
                    System.out.print(node.data + " -> ");
                }
            }
        }
    }

    // 校验参数方法
    private void checkIndex(int index) {
        if (index < 0 || index >= getSize()) {
            throw new IllegalArgumentException("参数 index 不合法");
        }
    }

    public static void main(String[] args) {
        MyLinkedList<Integer> linkedList = new MyLinkedList<>();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        linkedList.add(4);
        linkedList.add(5);
        linkedList.show();
        linkedList.add(2, 1);
        linkedList.show();
        linkedList.remove(2);
        linkedList.show();
    }
}